package com.example.hookams

import android.app.Activity
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.example.hookams.LoginUtil.logout
import com.example.hookams.LoginUtil.saveLoginState

class LoginActivity : Activity() {
    var name: EditText? = null
    var password: EditText? = null
    private var className: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        name = findViewById<View>(R.id.name) as EditText
        password = findViewById<View>(R.id.password) as EditText
        className = intent.getStringExtra("extraIntent")
        if (className != null) {
            (findViewById<View>(R.id.text) as TextView).text = " 跳转界面：$className"
        }
    }

    fun login(view: View?) {
        if (name!!.text == null || password!!.text == null) {
            Toast.makeText(this, "请填写用户名 或密码", Toast.LENGTH_SHORT).show()
            return
        }
        if ("haha" == name!!.text.toString() && "123456" == password!!.text
                .toString()
        ) {
            saveLoginState(name!!.text.toString())
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
            if (className != null) {
                val componentName = ComponentName(this, className!!)
                val intent = Intent()
                intent.component = componentName
                startActivity(intent)
            }
            finish()
        } else {
            logout()
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show()
        }
    }
}